/**
 * 文件名：RequestLogUtil.java
 *
 * 创建人：虞云波 - .com
 *
 * 创建时间：2017年10月30日 上午9:52:58
 *
 * 版权所有：江苏晟邦网络科技有限公司
 */
package com.cbwl.eoas.cms.util;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cbwl.eoas.cms.model.sysconfig.SysLogDTO;

/**
 * <p>[描述信息：根据HttpServletRequest工具类]</p>
 *
 * @author 虞云波 - yuyunbo@3856.cc
 * @version 1.0 Created on 2017年10月30日 上午9:52:58
 */
public class RequestLogUtil {

	
	private static Logger log = LoggerFactory.getLogger(RequestLogUtil.class);
	
	/**
	 * 
	 * <p>根据request 封装参数信息</p>
	 * 
	 * @param request
	 * @return
	 * @author: 虞云波 - yuyunbo@3856.cc 
	 * @date: Created on 2017年10月30日 上午10:07:50
	 */
	public static SysLogDTO getRequestSysLog(HttpServletRequest request){
		SysLogDTO bean = new SysLogDTO();
		if(request != null ){
			 String ua = request.getHeader("User-Agent");
			 bean.setSysLogType(getTypeByAgent(ua));
			 String requestUrl = request.getRequestURL().toString();//得到请求的URL地址
			 bean.setSysUrl(requestUrl);
			 String requestUri = request.getRequestURI();//得到请求的资源
			 bean.setSysUri(requestUri);
			 String queryString = "";//得到请求的URL地址中附带的参数
			 Map<String, String[]> params = request.getParameterMap();  
		        for (String key : params.keySet()) {  
		            String[] values = params.get(key);  
		            for (int i = 0; i < values.length; i++) {  
		                String value = values[i];
		                if(i==0){
		                	queryString += key+"="+value;
		                }else{
		                	queryString += "&"+key+"="+ value;
		                }
		            }  
		        }  
			 bean.setSysParam(queryString);
			 String remoteAddr = request.getRemoteAddr();//得到来访者的IP地址
			 bean.setSysClientIp(remoteAddr);
			 
			 String method = request.getMethod();//得到请求URL地址时使用的方法
			 bean.setSysHttpMethod(method);
			
			 return bean;
		}
		return bean;
	}
	
	
	public static String getTypeByAgent( String agent)    
	{    
		String flag = "403";    
	  
	    String[] keywords = { "android", "iPhone", "iPod", "iPad", "Windows Phone"};    
	  
	       //排除 Windows 桌面系统    
	        if (!agent.contains("Windows NT") || (agent.contains("Windows NT") && agent.contains("compatible; MSIE 9.0;")))    
	        {    
	            //排除 苹果桌面系统    
	            if (!agent.contains("Windows NT") && !agent.contains("Macintosh"))    
	            {    
	                for (String item : keywords)    
	                {    
	                    if (agent.contains(item) && agent.contains("android"))    
	                    {    
	                        return  "402";    
	                    }else if(agent.contains("iPhone") || agent.contains("iPod")||agent.contains("iPad")){
	                    	return  "401";
	                    }    
	                }    
	            }    
	        }  
	    return flag;    
	} 
	
	
	
	
}
